package com.leon.androidcoursedesign.ui.dialogs;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.beans.Course;
import com.leon.androidcoursedesign.utils.ValueUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.CourseUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.SQLiteHelperFactory;
import com.leon.androidcoursedesign.utils.dataBaseUtils.Tables;

public class CourseDialog extends AlertDialog implements View.OnClickListener {
    private TextView course_textView_title;
    private EditText
            course_editText_course_name,
            course_editText_teacher,
            course_editText_location,
            course_editText_period,
            course_editText_credit;

    private CourseUtils courseUtils;

    private final String title;
    private Course course = null;
    private boolean isEdit;

    public CourseDialog(@NonNull Context context, String title) {
        super(context);
        this.title = title;
        this.isEdit = false;
    }

    public CourseDialog(@NonNull Context context, String title, Course course) {
        super(context);
        this.title = title;
        this.course = course;
        this.isEdit = true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_add_dialog);

        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        course_textView_title = findViewById(R.id.course_textView_title);
        course_textView_title.setText(title);

        course_editText_course_name = findViewById(R.id.course_editText_course_name);
        course_editText_teacher = findViewById(R.id.course_editText_teacher);
        course_editText_location = findViewById(R.id.course_editText_location);
        course_editText_period = findViewById(R.id.course_editText_period);
        course_editText_credit = findViewById(R.id.course_editText_credit);

        // 如果该dialog为修改课程使用的话该值不能为null
        if (course != null) {
            course_editText_course_name.setText(course.courseName);
            course_editText_teacher.setText(course.teacher);
            course_editText_location.setText(course.location);
            course_editText_period.setText(course.period);
            course_editText_credit.setText(course.credit);
        }

        Button course_button_submit = findViewById(R.id.course_button_submit);
        course_button_submit.setOnClickListener(this);

        Button course_button_cancel = findViewById(R.id.course_button_cancel);
        course_button_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.course_button_cancel:
                dismiss();
                break;
            case R.id.course_button_submit:
                Course course = new Course();
                course.operationId = ValueUtils.getLoginId(getContext());
                course.courseName = course_editText_course_name.getText().toString();
                course.teacher = course_editText_teacher.getText().toString();
                course.location = course_editText_location.getText().toString();
                course.period = course_editText_period.getText().toString();
                course.credit = course_editText_credit.getText().toString();

                //判断输入信息是否完整
                if (course.courseName.equals("") || course.teacher.equals("") || course.location.equals("") ||
                        course.period.equals("") || course.credit.equals("")) {
                    Toast.makeText(getContext(), "请将课程信息填写完整", Toast.LENGTH_SHORT).show();
                    break;
                }

                if (courseUtils == null) {
                    SQLiteHelperFactory sqLiteHelperFactory = new SQLiteHelperFactory(getContext(), Tables.Course);
                    courseUtils = (CourseUtils) sqLiteHelperFactory.getInstance();
                }

                // 判断是否为修改课程信息
                if (!isEdit) {
                    if (courseUtils.checkCourse(course.courseName, course.operationId)) {
                        Toast.makeText(getContext(), course.courseName + " 已存在，请不要重复添加", Toast.LENGTH_SHORT).show();
                    } else {
                        course.itemId = ValueUtils.getRandomId();
                        boolean addState = courseUtils.addCourse(course);

                        Toast.makeText(getContext(), addState ? course.courseName + " 添加成功" : "添加失败,请检查输入内容是否正确", Toast.LENGTH_SHORT).show();

                        if (addState) {
                            if (onRefreshListener != null) {
                                onRefreshListener.onRefresh(course, false);
                            }
                            dismiss();
                        }
                    }
                } else {
                    // 获取触发长按监听item的数据的itemId
                    course.itemId = this.course.itemId;

                    // 修改Course表中的课程信息
                    boolean updateState = courseUtils.updateCourse(course);

                    Toast.makeText(getContext(), updateState ? course.courseName + " 修改成功" : "修改失败,请检查输入内容是否正确", Toast.LENGTH_SHORT).show();

                    if (updateState) {
                        if (onRefreshListener != null) {
                            // 刷新RecyclerView中的信息
                            onRefreshListener.onRefresh(course, true);
                        }
                        dismiss();
                    }
                }


                break;
            default:
                break;
        }
    }

    private OnRefreshListener onRefreshListener;

    public interface OnRefreshListener {
        /**
         * 刷新数据
         *
         * @param course    course
         * @param isEdit    是否为修改course数据
         */
        void onRefresh(Course course, boolean isEdit);

        // dialog的消失监听
        void onDismiss();
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    @Override
    public void dismiss() {
        if (courseUtils != null) {
            courseUtils.close();
        }

        if (onRefreshListener != null && !isEdit) {
            onRefreshListener.onDismiss();
        }

        super.dismiss();
    }
}
